﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TVRKOPF
    {

        public TVRKOPF()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVRKOPF();
            DataContext = MV;
            InitializeComponent();
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KUNDNR = "";
                VVKP = "";
                KEY = "";
                ZAHLBED = "";

            }
            else
            {
                KEY = ((VVRKOPF)MV).VRKRENR;
                KUNDNR = ((VVRKOPF)MV).VRKKUNDNR;
                VVKP = ((VVRKOPF)MV).VRKZESSION + ((VVRKOPF)MV).VRKVERBNR;
                ZAHLBED = ((VVRKOPF)MV).VRKZAHLBED;
            }
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                AUFART = "";
                return;
            }
            dwData = TBMain.NewRow();
            ((VVRKOPF)MV).SetUpNewRow(ref dwData);
            string _LFD = VSDDAT.SD22.GETNEXT("0022", "VR", "VRRENR", 1);
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKRENR"] = Helpers.Helper.Right("00000000" + _LFD, 8); ;
            dwData["VRKREART"] = AUFART.PadRight(2);
            dwData["VRKVB"] = KRS.PadRight(3);
            ((VVRKOPF)MV).Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            //触发TVR04
            //KEY = "";
            //KEY = dwData["VRKRENR"].ToString();
            IsNew = false;
        }


        public override void OnAppModeChg()
        {
            base.OnAppModeChg();
            if (KRS.Length > 1)
                return;
            switch (app.company)
            {
                case "TST":

                    KRS = MV.year.Substring(2, 2);
                    switch (app.AppMode)
                    {
                        case "TISSUE":
                            //KRS = "S" + MV.year.Substring(3, 1);
                            break;
                        case "TMODE":
                            //KRS = "M" + MV.year.Substring(3, 1);
                            break;

                    }
                    break;
            }
        }

        private void AUF_Enter(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 9)
                return;
            AUFTYP = ORDERNUM.Substring(0, 1);
            KRS = ORDERNUM.Substring(1, 2);
            LFD = ORDERNUM.Substring(3, 6);
            if (!IsLoaded)
                return;
            dwData = TBMain.NewRow();
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKVERBNR"] = ORDERNUM.Substring(1, 8);
            dwData["VRKZESSION"] = AUFTYP;
            SResult = ((VVRKOPF)MV).QueryVRKOPF(dwData, 6);
            MergeData(SResult, true, true);
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKVERBNR"] = " AND VRKVERBNR LIKE'" + KRS + LFD + "%'"; ;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            SResult = TMV.QueryVRKOPF(dwData, 4);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }

        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.bgWorker_RunWorkerCompleted(sender, e);
            vwMaster.View.MoveCurrentTo(-1);
        }

        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            base.bgWorker_DoWork(sender, e);
            EventDispatcher.Instance.RaiseStatus("开始留位", StatusEventType.StopProgress);
            if (IsVK)
            {
                ((VVRKOPF)MV).VKKPT(PM);
                return;
            }
            if (IsVV)
            {
                ((VVRKOPF)MV).VVVKVR(PM);
                return;
            }

            if (IsRest)
            {
                ((VVRKOPF)MV).VV2VKREST(PM);
                return;
            }
            if (IsVR)
            {
                ((VVRKOPF)MV).VKKPT(PM);
                ((VVRKOPF)MV).REFRESHVR(PM);
                return;
            }
        }

        public void VKKStartPaymentCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一个发票行");
                return;
            }
            PAYPAL = 1;
            PM["RENR"] = KEY;
            PM["PAYPAL"] = PAYPAL;
            PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
            PM["ORDERNUM"] = VVKP;
            PM["AUFTYP"] = "K";
            PM["ZAHLBED"] = ZAHLBED;
            PM["KUNDNR"] = KUNDNR;
            IsVR = false;
            IsVV = false;
            IsVK = true;
            IsRest = false;
            bgWorker.RunWorkerAsync();
        }

        public void VVStartPaymentCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一个发票行");
                return;
            }
            PAYPAL = 2;
            PM["RENR"] = KEY;
            PM["PAYPAL"] = PAYPAL;
            PM["ORDERNUM"] = VVKP;
            PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
            PM["ZAHLBED"] = ZAHLBED;
            PM["AUFTYP"] = "K";
            PM["KUNDNR"] = KUNDNR;
            IsVR = false;
            IsVV = true;
            IsVK = false;
            IsRest = false;

            bgWorker.RunWorkerAsync();
        }

        public void RESTPaymentCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一个发票行");
                return;
            }
            PAYPAL = 3;
            PM["RENR"] = KEY;
            PM["PAYPAL"] = PAYPAL;
            PM["ORDERNUM"] = VVKP;
            PM["ZAHLBED"] = ZAHLBED;
            PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
            PM["AUFTYP"] = "K";
            PM["KUNDNR"] = KUNDNR;
            IsVR = false;
            IsVV = false;
            IsVK = false;
            IsRest = true;
            bgWorker.RunWorkerAsync();
        }

        bool IsVR = false;
        bool IsVV = false;
        bool IsVK = false;
        bool IsCM = false;
        bool IsRest = false;

        public void RefreshVRCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                {
                    MessageBox.Show("请选择一个发票行");
                    return;
                }
                PM["RENR"] = KEY;
                PM["PAYPAL"] = PAYPAL;
                PM["ORDERNUM"] = VVKP;
                PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
                PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
                PM["ZAHLBED"] = ZAHLBED;
                PM["AUFTYP"] = "K";
                PM["KUNDNR"] = KUNDNR;
                IsVR = true;
                IsVV = false;
                IsVK = false;
                IsRest = false;
                bgWorker.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            //增加附加费和尾款的费用管理
        }

       
        
    }
}
